package com.example.t470p.facechecktickets.facesdk;

import android.content.Context;
import android.graphics.Bitmap;

import com.synface.facerecognize.algorithm.FaceRecognize;
import com.synface.facerecognize.interfaces.FaceInterface;
import com.synjones.handsetS8.IDCardReaderModule;

/**
 * Created by zhaodianbo on 2018-3-21.
 */
public class FaceSDKImplV1 implements IFaceSDK {
    FaceRecognize mFaceRecognize;
    long[] multithreadedSupport = new long[2];
    private static  FaceSDKImplV1 faceSDKImplV1;
    private FaceSDKImplV1(){
    }
    public static FaceSDKImplV1 getInstance(){
        if(faceSDKImplV1==null){
            faceSDKImplV1=new FaceSDKImplV1();
        }
        return faceSDKImplV1;
    }

    @Override
    public int init(Context context) {

        IDCardReaderModule idCardReaderModule= IDCardReaderModule.getInstance(context);
        if(!idCardReaderModule.isOpen()){
            idCardReaderModule.open();
        }

        long t=System.currentTimeMillis();//确保模块可以工作
        while(System.currentTimeMillis()-t<2000){
            String samv = idCardReaderModule.getSamvIDString();
            if(samv!=null && samv.length()>10)
                break;
        }
        mFaceRecognize = FaceRecognize.getInstance(context);
        int status = mFaceRecognize.SynFace_Init(multithreadedSupport);
        if(status>=0) return 0;
        return status;
    }

    @Override
    public Object getIDCardPhotoFeature(Bitmap bitmapIDCardPhoto) {
        if(bitmapIDCardPhoto==null) return null;

        try {
            return  mFaceRecognize.SynFace_GetFaceFeature(bitmapIDCardPhoto);
        } catch (FaceInterface.FaceException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Object getPresentPhotoFeature(Bitmap bitmapPresentPhoto) {
        if(bitmapPresentPhoto==null) return null;

        try {
            return  mFaceRecognize.SynFace_GetFaceFeature(bitmapPresentPhoto);
        } catch (FaceInterface.FaceException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int compareFeature(Object idcardPhotoFea, Object presentPhotoFea) {
        try {
            return mFaceRecognize.SynFace_CompareFeature((String) idcardPhotoFea,(String) presentPhotoFea);
        } catch (FaceInterface.FaceException e) {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public int deinit() {
        mFaceRecognize.SynFace_Deinit();
        return 0;
    }
}
